package com.htzos.base.generate.tool;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Iterator;
import java.util.Map;

import com.htzos.base.generate.bean.ColumnTypeEnum;
import com.htzos.base.generate.bean.TableBean;
import com.htzos.common.util.CharacterUtil;
import com.htzos.common.util.PropertiesUtil;

/**
 * 根据单个表生成对应的实体BEAN代码
 * 
 * @author xudezhi
 *
 */
public class GenerateBeanBySingleTable {

	
	StringBuffer packageBuffer = new StringBuffer();
	StringBuffer deftionBuffer = new StringBuffer();
	StringBuffer functionBuffer = new StringBuffer();

	/**
	 * 生成BEAN JAVA文件
	 * 
	 * @param tableName
	 * @param columns
	 * @param savePath
	 * @return
	 * @throws IOException 
	 */
	public String generateBeanFile(String packageStr, String tableName,String pkName,
			Map<String, TableBean> columns,String savePath) throws IOException {

		if (null == columns || columns.size() == 0) {
			return "Columns is null!";
		}
		File file = new File(savePath + CharacterUtil.returnBeanName(tableName) + ".java");
		if (file.exists()) {
			if(PropertiesUtil.FILEREPLACE==false){
				return "文件存在";
			}else{
				file.delete();
			}
		
		}

		Iterator<String> key = columns.keySet().iterator();
		
		OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file.getAbsoluteFile()), "UTF-8");
		
		// 写入类定义,及属性定义
		packageBuffer.append("package " + packageStr + ".bean;\r\n");
		packageBuffer.append("import java.util.Date;\r\n");
		packageBuffer.append("import java.text.ParseException;\r\n");
		packageBuffer.append("import java.text.SimpleDateFormat;\r\n");
		
		deftionBuffer.append("public class "
				+ CharacterUtil.returnBeanName(tableName) + " implements java.io.Serializable{\r\n");

		String columnName;
		String type;
		while (key.hasNext()) {
			columnName = key.next();
			type = columns.get(columnName).getColumnType();
			wirteStr(type, CharacterUtil.firstLowCase(columnName),columns.get(columnName).getRemarks());
		}
		
		//将生成好的BEAN类信息写入JAVA文件
		out.write(packageBuffer.toString());
		out.write(deftionBuffer.toString());
		out.write(functionBuffer.toString());
		out.write("}");
		out.close();

		return "success";
	}

	/**
	 * 返回类型对应的写入代码
	 * 
	 * @param type
	 * @param columnName
	 * @return
	 */
	private void wirteStr(String type, String columnName,String remark) {
		
		ColumnTypeEnum columnType = ColumnTypeEnum.instanceOf(type);
		
		String objType = columnType.getObjType();
		
		deftionBuffer.append("//"+remark+";\r\n");
		deftionBuffer.append("  private " + objType + " " + columnName + ";\r\n\n");
		functionBuffer.append("  public " + objType + " get"+ CharacterUtil.firstUpperCase(columnName) + "(){\r\n");
		functionBuffer.append("  	return this." + columnName + ";\r\n");
		functionBuffer.append("  }\r\n\n");
		functionBuffer.append("  public void set"+ CharacterUtil.firstUpperCase(columnName) + "(" + objType + " "+ columnName + "){\r\n");
		functionBuffer.append("     this." + columnName + "=" + columnName+";\r\n");
		functionBuffer.append("  }\r\n\n");
		
		//对于时间类型生成对应的String的获取和设置
		if("Date".equals(objType)){
			functionBuffer.append("    public String getStr"+ CharacterUtil.firstUpperCase(columnName) + "(){\r\n");
			functionBuffer.append("        SimpleDateFormat sdf=new SimpleDateFormat(\"yyyy-MM-dd HH:mm:ss\"); \r\n");
			functionBuffer.append("        return this." + columnName + "==null?\"\":sdf.format(this."+columnName+");\r\n");
			functionBuffer.append("    }\r\n\n");
			functionBuffer.append("    public void setStr"+ CharacterUtil.firstUpperCase(columnName) + "(String str"+ columnName + "){\r\n");
			functionBuffer.append("         SimpleDateFormat sdf=new SimpleDateFormat(\"yyyy-MM-dd HH:mm:ss\"); \r\n");
			functionBuffer.append("         try { \r\n");
			functionBuffer.append("             this." + columnName + "= sdf.parse(str"+columnName+");\r\n"); 
			functionBuffer.append("         } catch (ParseException e) { \r\n\n");
			functionBuffer.append("    	    }\r\n");
			functionBuffer.append("    }\r\n\n");
		}
		
	}

}
